package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/17 10:22
 * https://leetcode-cn.com/problems/battleships-in-a-board/
 */
public class Title419 {

    int n;

    public int countBattleships(char[][] board) {
        n = board.length;
        int count = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 'X') {
                    count++;
                    dfs(board, i, j);
                }
            }
        }
        return count;
    }

    private void dfs(char[][] board, int i, int j) {
        if (i < 0 || i >= n || j < 0 || j >= board[i].length || board[i][j] != 'X') {
            return;
        }
        board[i][j] = 'O';
        dfs(board, i - 1, j);
        dfs(board, i + 1, j);
        dfs(board, i, j - 1);
        dfs(board, i, j + 1);
    }

    public static void main(String[] args) {

    }
}
